namespace PiggyBankCashFlowApi.Middleware;

public class AuthenticationDiagnosticsMiddleware
{
    private readonly RequestDelegate _next;

    public AuthenticationDiagnosticsMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        // 记录请求头
        var authHeader = context.Request.Headers["Authorization"];
        Console.WriteLine($"Authorization Header: {authHeader}");

        // 记录用户信息
        if (context.User.Identity?.IsAuthenticated == true)
        {
            Console.WriteLine($"Authenticated User: {context.User.Identity.Name}");
            foreach (var claim in context.User.Claims)
            {
                Console.WriteLine($"Claim: {claim.Type} = {claim.Value}");
            }
        }
        else
        {
            Console.WriteLine("User is not authenticated");
        }

        await _next(context);
    }
}